﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XYW.WebSite;

namespace xyw.admincp
{
    public partial class FriendesAdd : System.Web.UI.Page
    {
        private XYW.BLL.News bllNews = new XYW.BLL.News();
        public string newsContent = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            XYW.BLL.News_Classify bllClassify = new XYW.BLL.News_Classify();
            this.ddl_Classify.DataSource = bllClassify.GetList("type=1");
            this.ddl_Classify.DataTextField = "classifyName";
            this.ddl_Classify.DataValueField = "classifyName";
            this.ddl_Classify.DataBind();
            if (SiteAbout.gInt("id") > 0)
            {
                XYW.Model.News modelNews = bllNews.GetModel(SiteAbout.gInt("id"));
                this.txt_Author.Text = modelNews.author;
                this.ddl_Classify.SelectedValue = modelNews.classify;
                this.txt_Department.Text = modelNews.department;
                newsContent = Server.HtmlDecode(modelNews.newsContent);
                this.txt_Outline.Text = modelNews.outline;
                this.txt_Title.Text = modelNews.title;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            XYW.Model.News modelNews = new XYW.Model.News();
            if (SiteAbout.gInt("id") > 0)
                modelNews = bllNews.GetModel(SiteAbout.gInt("id"));
            modelNews.author = this.txt_Author.Text.Trim();
            modelNews.classify = this.ddl_Classify.SelectedValue;
            modelNews.department = this.txt_Department.Text.Trim();
            if (FileUpload1.HasFile)
                modelNews.imageThumb = UploadImage(FileUpload1);//缩略图
            else if (SiteAbout.gInt("id") == -1)
                modelNews.imageThumb = "";
            modelNews.isImage = 0;
            modelNews.newsContent = this.hd_newsContent.Value;
            modelNews.outline = this.txt_Outline.Text.Trim();
            modelNews.pubDate = DateTime.Now;
            modelNews.showInHomepage = 0;
            modelNews.sortBy = 0;
            modelNews.title = this.txt_Title.Text.Trim();
            if (SiteAbout.gInt("id") == -1)
                modelNews.totalClick = 1;
            if (SiteAbout.gInt("id") > 0)
                bllNews.Update(modelNews);
            else
                bllNews.Add(modelNews);
            Response.Redirect("./Friendes.aspx");
        }

        private string UploadImage(FileUpload FU_Image)
        {
            Boolean fileOK = false;
            String path = Server.MapPath("../images/Friendes/");
            string Dir = "";
            String fileExtension = "";
            if (FU_Image.HasFile)
            {
                fileExtension = System.IO.Path.GetExtension(FU_Image.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }

            if (fileOK)
            {
                try
                {
                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);
                    string newname = DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileExtension;
                    FU_Image.PostedFile.SaveAs(path + newname);
                    Dir = "images/Friendes/" + newname.Replace("\\", "/");
                    //Label1.Text = "File uploaded!";
                }
                catch (Exception ex)
                {
                    //Label1.Text = "File could not be uploaded.";
                }
            }
            else
            {
                //Label1.Text = "Cannot accept files of this type.";
            }
            return Dir;
        }
    }
}